<?php
/**
* Project: VsPhpFm 
* Package: VsCore
* File: vssession.php
*
* Version: 1.0
* Date: 30th August, 2007
* ------------------------
*
* Author: Ivan i. Atanasov johnny2000@abv.bg
*
*/

// Default Expire Time: 30 min. (0 - No Expire)
define('SESSION_EXPIRE_TIME', 1800);

class VsSession {

	private static $_session = NULL;

	private static $_expireTime = NULL;

	private $_id = NULL;

	static function Instance() {
		if(!self::$_session)
			self::$_session = new VsSession();

		return self::$_session;

	}
	//=================================================

	private function __construct(){
		if(self::$_expireTime) {
			session_cache_limiter('private');
			session_cache_expire(self::$_expireTime/60); // In Minutes
		}

		session_start();

		if(!isset($_SESSION['vsSessionId']))
			$_SESSION['vsSessionId'] = $this->generateRandId();
		
		$this->_id = $_SESSION['vsSessionId'];

		$_SESSION['vsSessionBeginTime'] = time();
	}
	//=================================================

	static function setExpireTime($expireTime = SESSION_EXPIRE_TIME) {
		self::$_expireTime = $expireTime;
	}
	//================================================

	function assign($varName, $varValue) {
		$_SESSION[$varName] = $varValue;
	}
	//===================================================

	function remove($varName) {
		unset($_SESSION[$varName]);
	}
	//===================================================

	function id() {
		return $this->_id;
	}
	//===================================================

	function value($varName) {
		if(isset($_SESSION[$varName]))
			return $_SESSION[$varName];
		else
			return FALSE;
	}
	//===================================================

	function isExists($varName) {
		return isset($_SESSION[$varName]);
	}
	//=============================================================

	function isArray($varName) {
		return @is_array($_SESSION[$varName]);
	}
	//==============================================================

	function setIdFromCookie() {
		$_SESSION['vsSessionId'] = $_COOKIE['vsSessionId'];
	}
	//===============================================================

	function saveIdToCookie() {

	}
	//===============================================================

	function debug() {
		$html = '';
		foreach($_SESSION as $key => $value) {
			$html = '$_SESSION['.$key.'] = '.$value;

		}

		echo $html;
	}
	//===============================================================

	private function _isSet($varName) {
		return isset($_SESSION[$varName]);
	}
	//=============================================================

	private function generateRandId() {
		$length = 16;
		$randstr = "";
      		
		for($i=0; $i<$length; $i++) {
			$randnum = mt_rand(0,61);

			if($randnum < 10)
				$randstr .= chr($randnum+48);
			elseif($randnum < 36)
				$randstr .= chr($randnum+55);
			else
				$randstr .= chr($randnum+61);
		}
      		
		return md5($randstr);
	}
	//=====================================================

};

?>
